package com.duan.laugh.activiti.config;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.CustomProperty;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.UserTask;
import org.activiti.editor.language.json.converter.UserTaskJsonConverter;

import java.util.Map;

/**
 * <p>
 * 继承UserTaskJsonConverter并覆盖相应的解析方法
 * </p>
 *
 * @author liutf
 * @since 2019-10-22
 */
public class CustomUserTaskJsonConverter extends UserTaskJsonConverter {

    @Override
    protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap) {
        FlowElement flowElement = super.convertJsonToElement(elementNode, modelNode, shapeMap);
        UserTask userTask = (UserTask) flowElement;
        //将自己的属性添加到activiti自带的自定义属性中
        this.addParam(userTask,elementNode,"task_url","message_select","email_select");
        return userTask;
    }

    @Override
    protected void convertElementToJson(ObjectNode propertiesNode, BaseElement baseElement) {
        super.convertElementToJson(propertiesNode, baseElement);
    }

    /**
     * 将自己的属性添加到activiti自带的自定义属性中
     *
     * @param userTask
     * @param elementNode
     * @param params
     */
    private void addParam(UserTask userTask, JsonNode elementNode, String... params) {
        for (String param : params) {
            CustomProperty customProperty = new CustomProperty();
            customProperty.setName(param);
            customProperty.setSimpleValue(this.getPropertyValueAsString(param, elementNode));
            userTask.getCustomProperties().add(customProperty);
        }
    }
}
